﻿#include "esayview.h"
#include<QDebug>
#include<QPushButton>
#include "levelselect.h"
#include<QMessageBox>
#include<QDateTime>
EsayView::EsayView()
{
    //设置鼠标移动时不用按
    setMouseTracking(true);
    setCursor(Qt::BlankCursor);
    setWindowTitle("捕鱼达人");
    setWindowIcon(QIcon(":/image/fish1.png"));
    this->resize(1295,864);
    QPixmap pix;
    pix.load(":/image/seaworld2.png");
    this->setBackgroundBrush(QBrush(pix));
    scene=new QGraphicsScene;
    //设置背景大小
    scene->setSceneRect(0,0,this->width()-2,this->height()-2);
    //默认中心为坐标原点(将图片画四份)
    this->setScene(scene);

    QPushButton *l=new QPushButton;
    l->setParent(this);
    l->setFont(QFont("华文新魏",16));
    l->setFlat(true);
    l->move(10,10);
    l->setText("返回");
    connect(l,&QPushButton::clicked,this,[=](){
        levelselect *level=new levelselect;
        this->hide();
        level->show();
    });

    //绘制大炮
    gun=new QGun(":/image/pao5.png",scene);
    gun->setPos(this->width()/2,this->height());
    //scene->addItem(gun);

    //绘制鱼
    fish1=new QFish(":/image/shark_r2.png",scene);

    //fish1->setPos(-qrand()%-400-200,50);
    //scene->addItem(fish1);

    fish2=new QFish(":/image/shark_r2.png",scene);
    //fish2->setPos(-qrand()%-400-200,200);
   // scene->addItem(fish2);

    fish3=new QFish(":/image/shark_r2.png",scene);
    //fish3->setPos(-qrand()%-400-200,400);
    //scene->addItem(fish3);

    fish4=new QFish(":/image/shark_r2.png",scene);
    //fish4->setPos(-qrand()%-400-200,600);
    //scene->addItem(fish4);

    fish5=new QFish(":/image/shark_r2.png",scene);
    //fish5->setPos(-qrand()%-400-200,800);
    //scene->addItem(fish5);

    //定时器
    timer=new QTimer;
    connect(timer,&QTimer::timeout,scene,&QGraphicsScene::advance);
    timer->start(100);//每0.1s就会执行scene下重写了advance的方法 可以一次性控制全部的鱼
}

void EsayView::resizeEvent(QResizeEvent *event)
{
    this->setBackgroundBrush(QBrush(QPixmap(":/image/seaworld2.png").scaled(event->size())));
}

void EsayView::mouseMoveEvent(QMouseEvent *event)
{
    QPoint p;
    p=event->pos();
    QLine line(this->width()/2,this->height(),p.x(),p.y());
    QLineF lineF(line);
    //angle为线和x正半轴的夹角
    //需要的到线和y负半轴的夹角(角度取负)
    gun->setRotation(90-lineF.angle());

}

void EsayView::mousePressEvent(QMouseEvent *event)
{
    //0.5s只能按一次
    static QDateTime clickLastTime;
    QDateTime curDataTime = QDateTime::currentDateTime();
    if(clickLastTime.addMSecs(500)>curDataTime)
    {
        return;
    }
    clickLastTime = curDataTime;
    static int count=0;
    count++;
    QPoint p;
    p=event->pos();
    QLine line(this->width()/2,this->height(),p.x(),p.y());
    QLineF lineF(line);
    QBullet *bullet=new QBullet(":/image/B5.png",scene,lineF.angle());

    if(count>30){
        QMessageBox::warning(this,"警告","没子弹了请充钱");
        levelselect *level=new levelselect;
        this->hide();
        level->show();
        count=0;
    }
}

